package com.partsoft.dits.message;

import java.util.Date;

import org.springframework.util.Assert;

import com.partsoft.dits.utils.CalendarUtils;

/**
 * 基本的消息头实现
 */
public class BaseMessageHeader implements MessageHeader, Cloneable {
	
	private String messageId;
	
	private String messageCode;
	
	private String from;
	
	private DataAction defaultAction;
	
	private Date createTime = new Date();
	
	public BaseMessageHeader() {}
	
	public BaseMessageHeader(MessageHeader header) {
		Assert.notNull(header);
		this.messageId = header.getMessageId();
		this.messageCode = header.getMessageCode();
		this.from = header.getFrom();
		this.defaultAction = header.getDefaultAction();
		this.createTime = header.getCreateTime();
	}
	
	public BaseMessageHeader(String messageId, String messageCode, String from, DataAction defaultAction) {
		this(messageId, messageCode, from, defaultAction, new Date());
	}
	
	public BaseMessageHeader(String messageId, String messageCode, String from,
			DataAction defaultAction, Date createTime) {
		this.messageId = messageId;
		this.messageCode = messageCode;
		this.from = from;
		this.defaultAction = defaultAction;
		this.createTime = createTime;
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public String getMessageCode() {
		return messageCode;
	}

	public void setMessageCode(String messageCode) {
		this.messageCode = messageCode;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public DataAction getDefaultAction() {
		return defaultAction;
	}

	public void setDefaultAction(DataAction defaultAction) {
		this.defaultAction = defaultAction;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	public String getCreateDateTime() {
		return CalendarUtils.toXmlDateTime(this.getCreateTime());
	}
	
	@Override
	public BaseMessageHeader clone() {
		try {
			return (BaseMessageHeader) super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}

	@Override
	public String toString() {
		return "head[id=" + messageId + ", code="
				+ messageCode + ", from=" + from + ", action="
				+ defaultAction + ", ctime=" + createTime + "]";
	}
	
}
